/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package daos;

import daos.exceptions.NonexistentEntityException;
import entities.Fecha;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entities.Quote;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Juan
 */
public class FechaJpaController implements Serializable {
    private static FechaJpaController instance;
    
    public synchronized static FechaJpaController getInstance(){
        if(instance == null){
            instance = new FechaJpaController();
        }
        return instance;
    }

    public FechaJpaController() {
        this.emf = Persistence.createEntityManagerFactory("JavaApplication9PU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Fecha fecha) {
        if (fecha.getQuoteCollection() == null) {
            fecha.setQuoteCollection(new ArrayList<Quote>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Quote> attachedQuoteCollection = new ArrayList<Quote>();
            for (Quote quoteCollectionQuoteToAttach : fecha.getQuoteCollection()) {
                quoteCollectionQuoteToAttach = em.getReference(quoteCollectionQuoteToAttach.getClass(), quoteCollectionQuoteToAttach.getId());
                attachedQuoteCollection.add(quoteCollectionQuoteToAttach);
            }
            fecha.setQuoteCollection(attachedQuoteCollection);
            em.persist(fecha);
            for (Quote quoteCollectionQuote : fecha.getQuoteCollection()) {
                Fecha oldIddateOfQuoteCollectionQuote = quoteCollectionQuote.getIddate();
                quoteCollectionQuote.setIddate(fecha);
                quoteCollectionQuote = em.merge(quoteCollectionQuote);
                if (oldIddateOfQuoteCollectionQuote != null) {
                    oldIddateOfQuoteCollectionQuote.getQuoteCollection().remove(quoteCollectionQuote);
                    oldIddateOfQuoteCollectionQuote = em.merge(oldIddateOfQuoteCollectionQuote);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Fecha fecha) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Fecha persistentFecha = em.find(Fecha.class, fecha.getId());
            Collection<Quote> quoteCollectionOld = persistentFecha.getQuoteCollection();
            Collection<Quote> quoteCollectionNew = fecha.getQuoteCollection();
            Collection<Quote> attachedQuoteCollectionNew = new ArrayList<Quote>();
            for (Quote quoteCollectionNewQuoteToAttach : quoteCollectionNew) {
                quoteCollectionNewQuoteToAttach = em.getReference(quoteCollectionNewQuoteToAttach.getClass(), quoteCollectionNewQuoteToAttach.getId());
                attachedQuoteCollectionNew.add(quoteCollectionNewQuoteToAttach);
            }
            quoteCollectionNew = attachedQuoteCollectionNew;
            fecha.setQuoteCollection(quoteCollectionNew);
            fecha = em.merge(fecha);
            for (Quote quoteCollectionOldQuote : quoteCollectionOld) {
                if (!quoteCollectionNew.contains(quoteCollectionOldQuote)) {
                    quoteCollectionOldQuote.setIddate(null);
                    quoteCollectionOldQuote = em.merge(quoteCollectionOldQuote);
                }
            }
            for (Quote quoteCollectionNewQuote : quoteCollectionNew) {
                if (!quoteCollectionOld.contains(quoteCollectionNewQuote)) {
                    Fecha oldIddateOfQuoteCollectionNewQuote = quoteCollectionNewQuote.getIddate();
                    quoteCollectionNewQuote.setIddate(fecha);
                    quoteCollectionNewQuote = em.merge(quoteCollectionNewQuote);
                    if (oldIddateOfQuoteCollectionNewQuote != null && !oldIddateOfQuoteCollectionNewQuote.equals(fecha)) {
                        oldIddateOfQuoteCollectionNewQuote.getQuoteCollection().remove(quoteCollectionNewQuote);
                        oldIddateOfQuoteCollectionNewQuote = em.merge(oldIddateOfQuoteCollectionNewQuote);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = fecha.getId();
                if (findFecha(id) == null) {
                    throw new NonexistentEntityException("The fecha with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Fecha fecha;
            try {
                fecha = em.getReference(Fecha.class, id);
                fecha.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The fecha with id " + id + " no longer exists.", enfe);
            }
            Collection<Quote> quoteCollection = fecha.getQuoteCollection();
            for (Quote quoteCollectionQuote : quoteCollection) {
                quoteCollectionQuote.setIddate(null);
                quoteCollectionQuote = em.merge(quoteCollectionQuote);
            }
            em.remove(fecha);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Fecha> findFechaEntities() {
        return findFechaEntities(true, -1, -1);
    }

    public List<Fecha> findFechaEntities(int maxResults, int firstResult) {
        return findFechaEntities(false, maxResults, firstResult);
    }

    private List<Fecha> findFechaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Fecha.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Fecha findFecha(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Fecha.class, id);
        } finally {
            em.close();
        }
    }

    public int getFechaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Fecha> rt = cq.from(Fecha.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
